package com.njlime.member.repository.app;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.member.entiies.MemberCollection;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017/10/9.
 */
@Repository
public class AppMemberCollectionDaoImpl extends BaseDaoImpl<MemberCollection>{
    /**
     * 分页查询用户收藏
     * @param memberCollection
     * @return
     */
    public PageBean getMemberCollectionList(PageBean pb, MemberCollection memberCollection) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from (");
        sb.append("SELECT ");
        sb.append("a.id,a.collection_type,a.member_id,a.business_id,a.create_time,a.business_title, (CASE " +
                "WHEN a.business_type = 1 THEN ti.publish_status " +
                "WHEN a.business_type = 2 THEN tl.publish_status " +
                "WHEN a.business_type = 3 THEN tc.publish_status " +
                "END " +
                ") col_status ");
        sb.append(" from t_member_collection a ");
        sb.append("LEFT JOIN t_information ti ON ti.id = a.business_id ");
        sb.append("LEFT JOIN t_live tl ON tl.id = a.business_id ");
        sb.append("LEFT JOIN t_community tc ON tc.id = a.business_id ");
        sb.append(" WHERE 1 = 1  ");
        if (memberCollection.getBusiness_title() != null) {
            sb.append("  and a.business_type = ? ");
            arrList.add(memberCollection.getBusiness_title());
        }
        if (memberCollection.getMember_id() != null) {
            sb.append("  and a.member_id = ? ");
            arrList.add(memberCollection.getMember_id());
        }
        if (memberCollection.getBusiness_id() != null) {
            sb.append("  and a.business_id = ? ");
            arrList.add(memberCollection.getBusiness_id());
        }
        if (!StringUtil.isEmpty(memberCollection.getBusiness_title())) {
            sb.append(" and a.business_title like concat('%',?,'%') ");
            arrList.add(memberCollection.getBusiness_title());
        }
        sb.append(") t where col_status = 1 ");
        int num = getListJdbcSize(sb.toString(), arrList);
        pb.setTotalCount(num);
        pb.setTotalPage(pb.getTotalPage());
        if (num > 0) {
            sb.append(" order by t.id desc limit ?,?");
            arrList.add((pb.getCurPage() - 1) * pb.getPageSize());
            arrList.add(pb.getPageSize());
            pb.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pb;
    }

    /**
     * 收藏详情
     * @param uid
     * @param business_id
     * @param business_type
     * @return
     */
    public Map<String, Object> get(Integer uid, Integer business_id, Integer business_type) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_member_collection where 1=1 ");
        if (uid != null && uid > 0) {
            sb.append("and member_id  = ? ");
            arrList.add(uid);
        }
        if (business_id != null && business_id > 0) {
            sb.append("and business_id  = ? ");
            arrList.add(business_id);
        }
        if (business_type != null && business_type > 0) {
            sb.append("and business_type  = ? ");
            arrList.add(business_type);
        }
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 收藏数量
     * @param business_id
     * @param business_type
     * @return
     */
    public int count(Integer business_id, Integer business_type){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select count(*) from t_member_collection where business_id = ? and business_type = ? ");
        arrList.add(business_id);
        arrList.add(business_type);
        return Integer.valueOf(findCount(sb.toString(),arrList).toString());
    }

}
